package com.fcscs.ecms.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.ecms.dao.ECMSGuestRequestDao;
import com.fcscs.ecms.domain.GuestRequest;

/**
 * @author pvttinh
 * 
 */

@SuppressWarnings("unchecked")
@Repository("guestRequestDao")
public class ECMSGuestRequestDaoHibernate implements ECMSGuestRequestDao {
	
	@Autowired
	private ConnectionManager connMgr;
	//
	private static final String find_by_request_type = 
			"FROM GuestRequest gr "
		  + "WHERE gr.orgId = :orgId "
		  + "AND gr.propertyId = :propertyId "
		  + "AND gr.guestProfileId = :guestProfileId "
		  + "AND gr.reservationId = :reservationId "
		  + "AND gr.requestType = :requestType ";
	
	//
	private static final String find_list_by_request_type = 
			"FROM GuestRequest gr "
		  + "WHERE gr.orgId = :orgId "
		  + "AND gr.propertyId = :propertyId "
		  + "AND gr.guestProfileId = :guestProfileId "
		  + "AND gr.requestType = :requestType ";
	
	@Override
	public GuestRequest get(int id) throws Exception {
		// TODO Auto-generated method stub
		GuestRequest gr= null;
		try{
			gr = (GuestRequest) this.connMgr.getCurrentSession(DBLoc.GUEST).get(GuestRequest.class, id);
			if(null == gr){
				throw new Exception("GUEST REQUEST NOT EXIST");
			}
		}
		catch (Exception e) {
			// TODO: handle exception
			throw new Exception("GuestRequest.DAO.get() - " + e.getMessage());
		}
		
		return gr;
	}
	
	@Override
	public List<GuestRequest> findBy(int orgId, int propertyId,
			int guestProfileId, int reservationId, String requestType)
			throws Exception {
		// TODO Auto-generated method stub
		List<GuestRequest> list = null;
		try{
			Query query = this.connMgr.getCurrentSession(DBLoc.GUEST).createQuery(
					ECMSGuestRequestDaoHibernate.find_by_request_type);
			query.setInteger("orgId", orgId);
			query.setInteger("propertyId", propertyId);
			query.setInteger("guestProfileId", guestProfileId);
			query.setInteger("reservationId", reservationId);
			query.setString("requestType", requestType);
			
			list = query.list();
			if(list.isEmpty()){
				throw new Exception("GUEST REQUEST TYPE " + requestType + " NOT FOUND");
			}
		}catch (Exception e) {
			// TODO: handle exception
			throw new Exception("GuestRequest.DAO.findBy(requestType) - " + e.getMessage());
		}				
		
		return list;
	}
	
	@Override
	public List<GuestRequest> findListBy(int orgId, int propertyId,
			int guestProfileId, String requestType) throws Exception {
		// TODO Auto-generated method stub
		List<GuestRequest> list = null;
		try{
			Query query = this.connMgr.getCurrentSession(DBLoc.GUEST).createQuery(
					ECMSGuestRequestDaoHibernate.find_list_by_request_type);
			query.setInteger("orgId", orgId);
			query.setInteger("propertyId", propertyId);
			query.setInteger("guestProfileId", guestProfileId);
			query.setString("requestType", requestType);
			
			list = query.list();
			if(list.isEmpty()){
				throw new Exception("GUEST REQUEST TYPE " + requestType + " NOT FOUND");
			}
		}catch (Exception e) {
			// TODO: handle exception
			throw new Exception("GuestRequest.DAO.findListBy(requestType) - " + e.getMessage());
		}				
		
		return list;
	}

	@Override
	public GuestRequest update(GuestRequest gr) throws Exception {
		// TODO Auto-generated method stub
		try{
			this.connMgr.getCurrentSession(DBLoc.GUEST).update(gr);
		}catch (Exception e) {
			// TODO: handle exception
			throw new Exception("GuestRequest.DAO.update() - " + e.getMessage());
		}				
		
		return gr;
	}
}
